
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import router from './router'
import store from './store'
import axios from "axios"
import $ from 'jquery'
import * as echarts from 'echarts';
import VueContextMenu from 'vue-contextmenu'


Vue.config.productionTip = false
Vue.use(ElementUI)
Vue.use(VueContextMenu)
Vue.prototype.$axios = axios
Vue.prototype.$echarts = echarts

/**
 * 页面跳转判断用户是否登录
 */
router.beforeEach((to, from, next) => {
  if (to.path != "/login" && !store.state.userStore.isLogin && to.path != "/") {
    vm.$message({
      message: '请先登录',
      center: true,
      type: 'error'
    });
    next({
      path: "/login"
    });
  } else {
    next();
  }
})

/*实时获取sessionStorage的值*/
Vue.prototype.resetSetItem = function (key, newVal) {
  if (key === 'categoryNum') {
      // 创建一个StorageEvent事件
      var newStorageEvent = document.createEvent('StorageEvent');
      const storage = {
          setItem: function (k, val) {
              sessionStorage.setItem(k, val);

              // 初始化创建的事件
              newStorageEvent.initStorageEvent('setItem', false, false, k, null, val, null, null);

              // 派发对象
              window.dispatchEvent(newStorageEvent)
          }
      }
      return storage.setItem(key, newVal);
  }
}


/* eslint-disable no-new */
const vm = new Vue({
  el: '#app',
  router,
  store,
  components: {
    App
  },
  template: '<App/>'
})
